﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SevenAlgorithms.插值查找
{
    class MainProject
    {
        static void Main(string[] args)
        {
            int[] array = new int[] { -1, 0, 3, 5, 9, 12 };

            Console.WriteLine(ErFind(array, 13));
            Console.ReadKey();
        }
        public static int ErFind(int[] nums, int target)
        {
            //插值查找
            int left = 0, right = nums.Length - 1;

            while (left < right) 
            {
                //int mid = left + (right - left) / 2;
                int mid = left + ((right - left) / (nums[right] - nums[left])) * (target - nums[left]);
                Console.WriteLine(mid);
                if (nums[mid] == target)
                {
                    return mid;
                }
                else if(nums[mid] > target)
                {
                    right = mid - 1;
                }
                else
                {
                    left = mid + 1;
                }
            }
            return -1;
        }
    }
}
